package com.sdx.console.enrolledData.web;

import java.util.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.console.plan.entity.TDPlanningData;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.admin.core.util.*;
import com.sdx.common.utils.*;
import com.sdx.console.enrolledData.entity.*;
import com.sdx.console.enrolledData.service.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.ibeetl.admin.core.web.AuthorizedBaesController;


/**
 * 录取数据 接口
 */
@RequestMapping("/tDEnrolledData/tDEnrolledData")
@Controller
public class TDEnrolledDataController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(TDEnrolledDataController.class);
    private static final String MODEL = "/tDEnrolledData/tDEnrolledData";
    private static final String MAJORMODEL = "/tDEnrolledData/major";

    private static final String SQL = "tDEnrolledData.tDEnrolledData";


    @Resource
	private TDEnrolledDataService tDEnrolledDataService;

    @Resource
    private CorePlatformService platformService;

    @Resource
    private SQLManager sqlManager;

	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

        CoreOrg org = getLoginUserOrg(request);
        Integer accessType = platformService.getAccessType(request);
        Integer provinceId = null;
        if(accessType!=6){
            provinceId = org.getProvinceId();
        }
        view.addObject("provinceList",tDEnrolledDataService.selectProv(provinceId));
	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("tDEnrolledData.tDEnrolledData.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("tDEnrolledData.tDEnrolledData.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer id,Integer provinceId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        TDEnrolledData tDEnrolledData = tDEnrolledDataService.queryEnrolledDateById(id, provinceId);
        view.addObject("tDEnrolledData", tDEnrolledData);
		setPageParam(view,request);
		try {
			view.addObject("tDEnrolledDataJson", JacksonUtils.toJsonString(tDEnrolledData,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("tDEnrolledData.tDEnrolledData.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request,TDEnrolledData tdEnrolledData ) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        try {
            view.addObject("enrolledData", JacksonUtils.toJsonString(tdEnrolledData,JacksonUtils.DF1));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("tDEnrolledData.tDEnrolledData.query")
    @ResponseBody
    public JsonResult<PageQuery> list(TDEnrolledData condtion)
    {
        PageQuery page = condtion.getPageQuery();
        condtion.setType(0);
        if(condtion.getProvinceId()!=null&&condtion.getMajorTypeId()!=null&&condtion.getYear()!=null) {
            tDEnrolledDataService.queryByCondition(page);
        }
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew, TDEnrolledData tDEnrolledData, HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		CoreUser user = getLoginUser(request);
        tDEnrolledData.setChecker(user.getName());
        tDEnrolledData.setCheckTime(new Date());
        tDEnrolledData.setType(0);
    	if (isNew) {
            Map<String,Object> map = new HashMap<>();
            map.put("provinceId",tDEnrolledData.getProvinceId());
            map.put("year",tDEnrolledData.getYear());
            map.put("majorTypeId",tDEnrolledData.getMajorTypeId());
            map.put("batchId",tDEnrolledData.getBatchId());
            List<BaseEntity> list = tDEnrolledDataService.selectBatch(map);
            if (list.size()>0) {
                BaseEntity baseEntity = list.get(0);
                tDEnrolledData.setH5BatchId((Integer)baseEntity.get("h5BatchId"));
            }
            tDEnrolledDataService.insertData(tDEnrolledData);
		} else {
			tDEnrolledDataService.updateData(tDEnrolledData);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("tDEnrolledData.tDEnrolledData.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class) TDEnrolledData tDEnrolledData, HttpServletRequest request
		)
    {

        return save(true,tDEnrolledData,request
		);
    }

    @PostMapping("/update.json")
    @Function("tDEnrolledData.tDEnrolledData.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class) TDEnrolledData tDEnrolledData, HttpServletRequest request
		) {
		return save(false,tDEnrolledData,request
		);
    }



    @GetMapping("/view.json")
    @Function("tDEnrolledData.tDEnrolledData.query")
    @ResponseBody
    public JsonResult<TDEnrolledData>queryInfo(Integer id) {
        TDEnrolledData tDEnrolledData = tDEnrolledDataService.queryById( id);
        return  JsonResult.success(tDEnrolledData);
    }

    @PostMapping("/delete.json")
    @Function("tDEnrolledData.tDEnrolledData.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        tDEnrolledDataService.batchDelTDEnrolledData1(idList);
        return JsonResult.success();
    }

    @RequestMapping("/getBatch.json")
    @ResponseBody
    public List<BaseEntity>  getBatch(Integer provinceId, Integer year, Integer majorTypeId) {
        List<BaseEntity> list = new ArrayList<>();
        if(provinceId!=null&&year!=null&&majorTypeId!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("provinceId",provinceId);
            map.put("year",year);
            map.put("majorTypeId",majorTypeId);
            list = tDEnrolledDataService.selectBatch(map);
        }
        return list;
    }



    //***************************专业************************************

    private void setMajorPageParam(ModelAndView view,HttpServletRequest request,Integer provinceId,Integer pId) {
        view.addObject("pId", pId);
        view.addObject("provinceId", provinceId);
    }


    @GetMapping("/majorIndex.do")
    @Function("tDEnrolledData.tDEnrolledData.query")
    @ResponseBody
    public ModelAndView majorIndex(HttpServletRequest request,Integer id,Integer provinceId) {
        ModelAndView view = new ModelAndView(MAJORMODEL+"/index.html") ;
        TDEnrolledData tdEnrolledData = tDEnrolledDataService.queryEnrolledDateById(id, provinceId);
        view.addObject("enrolled",tdEnrolledData);
        return view;
    }

    @PostMapping("/majorList.json")
    @Function("tDEnrolledData.tDEnrolledData.query")
    @ResponseBody
    public JsonResult<PageQuery> majorList(TDEnrolledData condtion)
    {
        PageQuery page = condtion.getPageQuery();
        condtion.setId(null);
        condtion.setType(1);
        tDEnrolledDataService.queryByCondition(page);
        return JsonResult.success(page);
    }


    @GetMapping("/majorEdit.do")
    @Function("tDEnrolledData.major.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer id,Integer provinceId,Integer pId) {
        ModelAndView view = new ModelAndView(MAJORMODEL+"/add.html");
        TDEnrolledData tdEnrolledData = tDEnrolledDataService.queryEnrolledDateById(id, provinceId);

        view.addObject("tdEnrolledData", tdEnrolledData);

        setMajorPageParam(view,request,provinceId,pId);
        try {
            view.addObject("tdEnrolledDataJson", JacksonUtils.toJsonString(tdEnrolledData,JacksonUtils.DF1));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return view;
    }

    @GetMapping("/majorAdd.do")
    @Function("tDEnrolledData.major.add")
    @ResponseBody
    public ModelAndView majoAadd(HttpServletRequest request,Integer provinceId,Integer pId) {
        ModelAndView view = new ModelAndView(MAJORMODEL+"/add.html");
        view.addObject("tDPlanningData", new TDPlanningData());
        setMajorPageParam(view,request,provinceId,pId);
        return view;
    }


    @PostMapping("/majoradd.json")
    @Function("tDEnrolledData.major.add")
    @ResponseBody
    public JsonResult majoradd(@Validated(ValidateConfig.ADD.class)TDEnrolledData tDEnrolledData,HttpServletRequest request,Integer pId)
    {
        return majorSave(true,tDEnrolledData,request,pId);
    }
    @PostMapping("/majorupdate.json")
    @Function("tDEnrolledData.major.edit")
    @ResponseBody
    public JsonResult<String> majorupdate(@Validated(ValidateConfig.UPDATE.class)TDEnrolledData tDEnrolledData,HttpServletRequest request,Integer pId
    ) {
        return majorSave(false,tDEnrolledData,request,pId);
    }

    private JsonResult majorSave(boolean isNew, TDEnrolledData tDEnrolledData, HttpServletRequest request, Integer pId ) {
        JsonResult result = JsonResult.success();
        CoreUser user = getLoginUser(request);
        tDEnrolledData.setChecker(user.getName());
        tDEnrolledData.setCheckTime(new Date());
        if (isNew) {
            tDEnrolledData.setType(1);
            TDEnrolledData enrolledData = tDEnrolledDataService.queryEnrolledDateById(pId, tDEnrolledData.getProvinceId());
            tDEnrolledData.setYear(enrolledData.getYear());
            tDEnrolledData.setMajorTypeId(enrolledData.getMajorTypeId());
            tDEnrolledData.setBatchId(enrolledData.getBatchId());
            tDEnrolledData.setH5BatchId(enrolledData.getH5BatchId());
            tDEnrolledData.setUmId(enrolledData.getUmId());
            tDEnrolledData.setUnivCode(enrolledData.getUnivCode());
            tDEnrolledData.setUnivId(enrolledData.getUnivId());
            tDEnrolledData.setUnivName(enrolledData.getUnivName());
            tDEnrolledDataService.insertData(tDEnrolledData);
        } else {
            tDEnrolledDataService.updateData(tDEnrolledData);
        }
        return result;
    }

    @RequestMapping("/check.json")
    @ResponseBody
    public JsonResult  check(Integer id,HttpServletRequest request,Integer provinceId){
        JsonResult result = JsonResult.success();
        CoreUser user = getLoginUser(request);
        TDEnrolledData tDEnrolledData = new TDEnrolledData();
        tDEnrolledData.setId(id);
        tDEnrolledData.setProvinceId(provinceId);
        tDEnrolledData.setChecker(user.getName());
        tDEnrolledData.setCheckTime(new Date());
        tDEnrolledDataService.updateData(tDEnrolledData);
        return result;
    }


    @RequestMapping("/updateEnrolledData.json")
    @ResponseBody
    public JsonResult  updateEnrolledData(TDEnrolledData tDEnrolledData,Integer type,HttpServletRequest request){
        JsonResult result = JsonResult.success();
        CoreUser user = getLoginUser(request);
        tDEnrolledData.setChecker(user.getName());
        tDEnrolledData.setCheckTime(new Date());
        String sql ="";
        if(type==1){
            sql = SQL + ".updateEnrolled";
        }else if(type==2){
            sql = SQL + ".updateLowScore";
        }else if(type==3){
            sql = SQL + ".updateAverageScore";
        }else if(type==4){
            sql = SQL + ".updateHighScore";
        }
        sqlManager.update(sql,tDEnrolledData);
        return result;
    }



}
